﻿using RpcClient.Interface;
using RpcClient.Rabbitmq.Model;
using RpcHelper;
using RpcModel;
using RpcSync.Collect;
using RpcSync.Service.Interface;

namespace RpcSync.Service.Service
{
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class DeadQueueService : IDeadQueueService
    {
        private IDeadQueueCollect _DeadQuery;
        private IBroadcastErrorCollect _Error;
        public DeadQueueService(IDeadQueueCollect deadQuery, IBroadcastErrorCollect error)
        {
            this._Error = error;
            this._DeadQuery = deadQuery;
        }

        public void Init()
        {
            if (_DeadQuery.IsInit)
            {
                _DeadQuery.BindQueue("Rpc_Dead_Queue", _DeadMsg);
            }
        }

        private void _DeadMsg(DeadQueueMsg msg, string routeKey, BroadcastType msgType)
        {
            if (msg.Msg == null || msg.Msg.Msg == null)
            {
                new WarnLog("rpc.queue.msg.format.error", "广播消息格式错误!", "Rpc_Broadcast")
                                {
                                        { "RouteKey",routeKey},
                                        { "Msg",msg.MsgBody},
                                        { "BroadcastType",msgType}
                                }.Save();
                return;
            }
            _Error.AddErrorLog(msg.Msg, routeKey, msgType);
        }
    }
}
